package exam.huawei;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/15 22:14
 */
public class HJ45 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        for (int i = 0; i < N; i++) {
            String str = in.next();
            System.out.println(maxBeautyNum(str));
        }
    }

    private static int maxBeautyNum(String str) {
        int beautyNum = 0;
        int[] map = new int[26];
        for (int i = 0; i < str.length(); i++) {
            map[str.charAt(i) - 'a']++;
        }
        Arrays.sort(map);
        int num = 26;
        for (int i = 25; i >= 0; i--) {
            if(map[i] != 0){
                beautyNum += map[i] * num;
                num--;
            }else{
                break;
            }
        }
        return beautyNum;
    }
}
